home *** CD-ROM | disk | FTP | other *** search
- #ifdef RISC_OS
- /* c.armtxt --- aka c.textwindow
- ---Tiggr & Graham
-
- ******************************************************************************
-
- Example: 80 * 24 screen, 200 row buffer, with history, buffer NOT full
-
- inclusive pointers exclusive pointers
- +----------+
- buf_start | |
- | |
- | |
- +----------+
- buf_end - 24 = scr_start | |
- | screen |
- | |
- +----------+
- | | buf_end
- | |
- | |
- +----------+
- buf_num_rows = 200
-
- ******************************************************************************
-
- Example: 80 * 24 screen, 200 row buffer, buffer is FULL
-
-
- inclusive pointers exclusive pointers
- +----------+
- | |
- | | decreasing
- | | rownumbers
- +----------+ ^
- buf_end - 24 = scr_start | | |
- | screen | |
- | | |
- +----------+
- buf_end = buf_start | | buf_end
- | |
- | |
- +----------+
- buf_num_rows = 200
-
- ******************************************************************************/
-
- typedef enum { /* event types */
- wimp_ENULL, /* null event */
- wimp_EREDRAW, /* redraw event */
- wimp_EOPEN,
- wimp_ECLOSE,
- wimp_EPTRLEAVE,
- wimp_EPTRENTER,
- wimp_EBUT, /* mouse button change */
- wimp_EUSERDRAG,
- wimp_EKEY,
- wimp_EMENU,
- wimp_ESCROLL,
- wimp_ELOSECARET,
- wimp_EGAINCARET,
- wimp_ESEND = 17, /* send message, don't worry if it doesn't arrive */
- wimp_ESENDWANTACK = 18, /* send message, return ack if not acknowledged */
- wimp_EACK = 19 /* acknowledge receipt of message. */
- } wimp_etype;
-
- typedef enum { /* event type masks */
- wimp_EMNULL = 1 << wimp_ENULL,
- wimp_EMREDRAW = 1 << wimp_EREDRAW,
- wimp_EMOPEN = 1 << wimp_EOPEN,
- wimp_EMCLOSE = 1 << wimp_ECLOSE,
- wimp_EMPTRLEAVE = 1 << wimp_EPTRLEAVE,
- wimp_EMPTRENTER = 1 << wimp_EPTRENTER,
- wimp_EMBUT = 1 << wimp_EBUT,
- wimp_EMUSERDRAG = 1 << wimp_EUSERDRAG,
- wimp_EMKEY = 1 << wimp_EKEY,
- wimp_EMMENU = 1 << wimp_EMENU,
- wimp_EMSCROLL = 1 << wimp_ESCROLL
- } wimp_emask;
-
- #define ARTHUR_NEW_NAMES
-
- #include <Arthur.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <signal.h>
-
- /* no #ifdefs... */
- #define TRUE (0==0)
- #define FALSE (0!=0)
-
- /*****************************************************************************/
-
- static int MODULO (int a, int b)
- {
- return ((a + b) % b);
-
- if (a >= 0) return a%b;
- return (b + (a%b))%b;
- } /* MODULO */
-
- #ifdef NEVER
- #define MODULO(a, b) ((a) >= 0 ? ((a) % (b)) : (((b) + ((a) % (b))) % (b)))
- #endif
-
- #define ROW(r) MODULO ((r), buf_num_rows)
- #define SCREEN_START ROW (buf_end - scr_num_rows)
- #define BUFFER_ROW(log) ROW (log + buf_start)
- #define SCREEN(col, row) buffer[ROW(SCREEN_START + (row)) * num_cols + (col)]
- #define BUFFER(col, row) buffer[BUFFER_ROW (row) * num_cols + (col)]
- #define INCREASE(row) (row = ROW (row + 1))
-
- #define MAP_VALID FALSE
- #define MAP_INVALID TRUE
-
- #define GET_CHARACTER(x) (x).character
- #define GET_ATTRIBUTE(x) (x).attribute
- #define GET_MAP(x) (x).map
-
- #define SET_CHARACTER(x, c) (x).character = (c)
- #define SET_ATTRIBUTE(x, a) (x).attribute = (a)
- #define SET_MAP(x, m) (x).map = (m)
-
- #ifndef Wimp_CloseDown /* hideous... */
- #define Wimp_CloseDown 0x400dd
- #endif
-
- #define MAX(X,Y) ((X)>(Y)?(X):(Y))
- #define MIN(X,Y) ((X)<(Y)?(X):(Y))
-
- #define NUM_VPIX 32
- #define NUM_HPIX 16
-
- #define INPUT_BUFFER_SIZE 256
-
- #define TASK_ID ('T'+('A'<<8)+('S'<<16)+('K'<<24))
-
- /*****************************************************************************/
-
- /* Buffer */
- typedef struct BUFFER_CELL {
- char character, attribute, map, preserved;
- } BUFFER_CELL;
-
- static BUFFER_CELL *buffer;
- static int buf_start, buf_end, buf_num_rows;
-
- /* Input buffer */
- static int input_buffer[INPUT_BUFFER_SIZE];
- static int in_buf_end = 0, in_buf_next = 0;
-
- /* Screen */
- static int scr_num_rows;
- static int cursor_col, cursor_row, cursor_on = FALSE, cursor_state,
- cursor_interval, cursor_nexttime, cursor_type, real_cursor_on;
-
- /* General & Wimp */
- static int num_cols, handle, num_hpix = NUM_HPIX, num_vpix = NUM_VPIX;
- static int update_pending = FALSE, extent_base = 0, screen_base = 0;
- static char *line;
- static int intch = -1;
-
- /*****************************************************************************/
-
- static int
- monotomic_time (void)
- {
- art_reg_set r;
-
- (void) art_swix (0x42, &r);
- return (r.r[0]);
- } /* monotomic_time */
-
- /*****************************************************************************/
-
- static void
- set_caret (void)
- {
- art_reg_set r;
-
- r.r[0] = handle, r.r[1] = -1, r.r[2] = r.r[3] = r.r[5] = 0;
- r.r[4] = 1 << 25;
- (void) art_swix (Wimp_SetCaretPosition, &r);
- } /* set_caret */
-
- /*****************************************************************************/
-
- void
- text_closedown (void)
- {
- art_reg_set r;
-
- (void) art_swix (Wimp_CloseDown, &r);
- } /* text_closedown */
-
- /*****************************************************************************/
-
- /* TEXT coordinates: (INCLUSIVE, EXCLUSIVE, EXCLUSIVE, INCLUSIVE) */
-
- static void
- invalidate (int lx, int by, int rx, int ty)
- {
- art_reg_set r;
- int _lx = MAX (lx, 0),
- _by = MIN (by, buf_num_rows),
- _rx = MIN (rx, num_cols),
- _ty = MAX (ty, 0);
-
- r.r[0] = handle;
- r.r[1] = _lx * num_hpix;
- r.r[2] = extent_base + screen_base -_by * num_vpix;
- r.r[3] = _rx * num_hpix;
- r.r[4] = extent_base + screen_base -_ty * num_vpix;
-
- (void) art_swix (Wimp_ForceRedraw, &r);
- } /* invalidate */
-
- /*****************************************************************************/
-
- void
- text_cursor (int type, int interval)
- {
- if (!type) cursor_on = FALSE;
- else {
- cursor_on = TRUE;
- real_cursor_on = FALSE;
- cursor_type = type; /* 1 = block, 2 = line */
- cursor_col = cursor_row = 0;
- cursor_interval = interval;
- cursor_nexttime = monotomic_time ();
- cursor_state = TRUE;
- }
- } /* text_cursor */
-
- /*****************************************************************************/
-
- void
- text_cursor_pos (int col, int row)
- {
- if (cursor_on) {
- if (real_cursor_on) {
- invalidate (cursor_col, cursor_row + 1, cursor_col + 1, cursor_row);
- }
- cursor_col = col;
- cursor_row = row;
- }
- } /* text_cursor_pos */
-
- /*****************************************************************************/
-
- void
- supress_cursor (int set_it_off) /* if !0 supress it */
- {
- if (set_it_off) {
- if (cursor_on) {
- cursor_state = 0;
- real_cursor_on = FALSE;
- invalidate (cursor_col, cursor_row + 1, cursor_col + 1, cursor_row);
- }
- }
- else real_cursor_on = cursor_on;
- }
-
- /*****************************************************************************/
-
-
- typedef struct {int a[3]; } unfortunate_hack;
-
- int
- text_init (int nc, int nr, int bufr, char *title, int inc, int inr, int ich)
- {
- art_reg_set r;
- art_open_block o;
- art_wind_block t = {0, 0, 0, 0, 0, 0, -1, 0x1010f,
- {7,2,7,0,3,1,12}, 0, 0, -1024, 1280, 0, 0x27000139,
- 0x3000, {0, 0}, {0,0,0,0,0,0,0,0,0,0,0,0}, 0};
- int i;
- unfortunate_hack *tit;
-
- tit = (unfortunate_hack *) &t.title[0];
- tit->a[0] = (int) title;
- tit->a[1] = (int) "";
- tit->a[2] = 1 + strlen (title);
-
- intch = ich;
- if (nc <= 0 || nr <= 0 || bufr < nr) return -1;
- if ((buffer = calloc (nc * bufr, sizeof (BUFFER_CELL))) == NULL) return -1;
- if ((line = malloc (nc + 1)) == NULL) goto ___;
- for (i = 0; i < nc * bufr; i++) SET_CHARACTER (buffer[i], ' ');
- scr_num_rows = nr, buf_num_rows = bufr, num_cols = nc;
- buf_start = 0, buf_end = ROW (scr_num_rows);
-
- r.r[0] = 200, r.r[1] = TASK_ID, r.r[2] = (int) title;
- if (art_swix (Wimp_Initialise, &r)) goto __;
- r.r[1] = (int) &t;
- t.exx0 = t.exy1 = 0, t.exx1 = num_hpix * nc, t.exy0 = -num_vpix * nr;
- t.x0 = 640 - inc * num_hpix / 2, t.x1 = 1280 - t.x0;
- t.y0 = 512 - inr * num_vpix / 2, t.y1 = 1024 - t.y0;
- if (art_swix (Wimp_CreateWindow, &r)) goto __;
- handle = r.r[0];
-
- o.wind_handle = handle, r.r[1] = (int) &o;
- if (art_swix (Wimp_GetWindowState, &r) || art_swix (Wimp_OpenWindow, &r))
- goto __;
- set_caret ();
- return 0;
- __:
- free (line);
- ___:
- free (buffer);
- return -1;
- } /* text_init */
-
- /*****************************************************************************/
-
- int
- text_putch (char ch, int attr, int col, int row)
- {
- if (col < 0 || col >= num_cols || row < 0 || row >= scr_num_rows)
- return -1;
- SET_CHARACTER (SCREEN (col, row), ch);
- SET_ATTRIBUTE (SCREEN (col, row), attr);
- SET_MAP (SCREEN (col, row), MAP_INVALID);
- update_pending = TRUE;
- return 0;
- } /* putch */
-
- /*****************************************************************************/
-
- static void
- redraw_window (art_redraw_block * b)
- {
- int i, j, rem_x, rem_y;
- int lx, by, rx, ty, clx, cby, crx, cty;
- art_reg_set r;
-
- art_gcol (0,7);
- r.r[1] = (int) b;
- (void) art_swix (Wimp_RedrawWindow, &r);
- while (r.r[0]) {
- /* graphics coordinates in work-area-extent */
- lx = b->scx + b->gx0 - b->x0; rx = lx + (b->gx1 - b->gx0);
- by = b->scy + b->gy0 - b->y1 - extent_base; ty = by + (b->gy1 - b->gy0);
- /* character coordinates in work-area-extent */
- clx = lx / num_hpix, crx = 1 + rx / num_hpix;
- if (crx > num_cols) crx = num_cols;
- if (clx < 0) clx = 0;
- cty = -ty / num_vpix; cby = 1 - (by / num_vpix);
- for (j = cty; j < cby; j++) {
- rem_x = b->gx0 - (lx%num_hpix);
- rem_y = b->gy1 - (ty%num_vpix) - (j-cty) * num_vpix - num_vpix/8;
- art_move (rem_x, rem_y);
- for (i = clx; i < crx; i++) {
- line[i - clx] = GET_CHARACTER (BUFFER (i, j));
- }
- line[i] = 0;
- r.r[0] = (int) line;
- (void) art_swix (OS_Write0, &r);
- if (cursor_on && real_cursor_on && cursor_state && j == cursor_row
- && cursor_col >= clx && cursor_col < crx) {
- art_move (rem_x + (cursor_col-clx) * num_hpix,
- rem_y - num_vpix + num_vpix / 8);
- art_gcol (4, 7);
- if (cursor_type == 2) art_plot (1, num_hpix - 1, 0);
- else if (cursor_type == 1) {
- art_plot (97, num_hpix - 1, num_vpix - 1);
- }
- art_gcol (0, 7);
- }
- }
- (void) art_swix (Wimp_GetRectangle, &r);
- }
- } /* redraw_window */
-
- /*****************************************************************************/
- /* Returns: 1 need to call text_update
- 0 normally
- -1 if program should exit */
-
- int
- text_poll (int immediate_exit)
- {
- int i, f;
- art_reg_set r;
- int a[64];
-
- do {
- r.r[0] = wimp_EMPTRENTER | wimp_EMPTRLEAVE
- | wimp_EMUSERDRAG | wimp_EMMENU;
- r.r[1] = (int) &a;
-
- #ifdef NEVER
- if (cursor_on) {
- r.r[2] = cursor_nexttime;
- (void) art_swix (0x400e1, &r);
- }
- else {
- r.r[0] |= wimp_EMNULL;
- #endif
-
- (void) art_swix (Wimp_Poll, &r);
-
- #ifdef NEVER
- }
- #endif
-
- switch (f = r.r[0]) {
- case 1: /* redraw */
- if (*a == handle)
- redraw_window ((art_redraw_block *) &a);
- break;
- case 2: /* open */
- if (*a == handle)
- (void) art_swix (Wimp_OpenWindow, &r);
- break;
- case 3: /* close */
- if (*a == handle) {
- (void) art_swix (Wimp_CloseDown, &r);
- if (immediate_exit) exit (0);
- else return -1;
- }
- break;
- case 6: /* mouse click */
- set_caret ();
- break;
- case 8: /* inkey */
- if (a[6] == 0x1cc || a[6] == 0x1fc) {
- /* F12 or <SHIFT><CTRL>F12 pressed */
- r.r[0] = a[6];
- (void) art_swix (0x400dc, &r);
- }
- else if (a[6] == intch) {
- /* intch is the interrupt key. Set to -1 if not wanted */
- in_buf_end = in_buf_next = 0;
- raise (SIGINT);
- }
- else if ((i = (in_buf_end + 1) % INPUT_BUFFER_SIZE) != in_buf_next) {
- in_buf_end = i;
- input_buffer[in_buf_end] = a[6];
- }
- break;
- case 10: /* scroll */
- if (*a == handle) {
- a[5] += ((a[8] == 1 || a[8] == -1) ? num_hpix * a[8]
- : ( a[8] == 0 ? 0 : (a[8] / 2 * (a[3] - a[1]))));
- a[6] += ((a[9] == 1 || a[9] == -1) ? num_vpix * a[9]
- : ( a[9] == 0 ? 0 : (a[9] / 2 * (a[4] - a[2]))));
- (void) art_swix (Wimp_OpenWindow, &r);
- }
- break;
- case 17: /* message */
- case 18: /* message */
- if (a[4] == 0) {
- (void) art_swix (Wimp_CloseDown, &r);
- if (immediate_exit) exit (0);
- else return -1;
- }
- break;
- default:
- break;
- }
- if (cursor_on && real_cursor_on &&
- monotomic_time () >= cursor_nexttime) {
- cursor_state = !cursor_state;
- cursor_nexttime = monotomic_time () + cursor_interval;
- invalidate (cursor_col, cursor_row + 1, cursor_col + 1, cursor_row);
- }
- } while (f);
- return (update_pending ? 1 : 0);
- } /* text_poll */
-
- /*****************************************************************************/
-
- int
- text_lookahead (void)
- {
- if (in_buf_next == in_buf_end) return -1;
- return input_buffer[(in_buf_next + 1) % INPUT_BUFFER_SIZE];
- } /* text_lookahead */
-
- /*****************************************************************************/
-
- int
- text_inkey (void)
- {
- if (in_buf_next == in_buf_end) {
- supress_cursor (0);
- return -1;
- }
- in_buf_next = (in_buf_next + 1) % INPUT_BUFFER_SIZE;
- supress_cursor (1);
- return input_buffer[in_buf_next];
- } /* text_inkey */
-
- /*****************************************************************************/
-
- void
- text_update (void)
- {
- int x, y, xl, xr;
-
- if (update_pending) {
- for (y = 0; y < scr_num_rows; y++) {
- x = 0;
- for (;;) {
- while ((x < num_cols) && (GET_MAP (SCREEN (x, y)) == MAP_VALID))
- x++;
- if (x >= num_cols) break;
- xl = xr = x;
- while (xr < num_cols && (GET_MAP (SCREEN (xr, y)) == MAP_INVALID)){
- SET_MAP (SCREEN (xr, y), MAP_VALID);
- xr++;
- }
- invalidate (xl, y + 1, xr, y);
- }
- }
- update_pending = FALSE;
- }
- } /* text_update */
-
- /*****************************************************************************/
-
- static void
- enlarge_extent (int dby, int dty)
- /* CALL ONLY WITH 0 OR -1 AS ARGUMENT VALUE!!! */
- {
- art_reg_set r;
- int b[30];
-
- b[0] = handle, r.r[1] = (int) &b;
- (void) art_swix (Wimp_GetWindowInfo, &r);
- b[12] += dby * num_vpix;
- extent_base = (b[14] += dty * num_vpix);
- screen_base -= ((dty == 0) ? num_vpix : 0);
- r.r[0] = handle;
- r.r[1] = (int) &b[11];
- (void) art_swix (Wimp_SetExtent, &r);
- } /* enlarge_extent */
-
- /*****************************************************************************/
-
- void
- text_open_screen (void)
- {
- art_reg_set r;
- int b[30];
-
- b[0] = handle, r.r[1] = (int) &b;
- (void) art_swix (Wimp_GetWindowInfo, &r);
- b[6] = b[12] - scr_num_rows * num_vpix;
- (void) art_swix (Wimp_OpenWindow, &r);
- } /* text_open_screen */
-
- /*****************************************************************************/
-
- void
- text_scroll (void)
- {
- int x;
- art_reg_set r;
- int b[30];
-
- /* Remember current window information */
- b[0] = handle, r.r[1] = (int) &b;
- (void) art_swix (Wimp_GetWindowInfo, &r);
-
- if (buf_start == buf_end) {
- INCREASE (buf_start);
- INCREASE (buf_end);
- enlarge_extent (-1, -1);
- for (x = 0; x < num_cols; x++) {
- SET_CHARACTER (SCREEN (x, scr_num_rows - 1), ' ');
- SET_ATTRIBUTE (SCREEN (x, scr_num_rows - 1), 0);
- SET_MAP (SCREEN (x, scr_num_rows - 1), MAP_INVALID);
- }
- } else {
- INCREASE (buf_end);
- enlarge_extent (-1, 0);
- }
-
- if (b[6] + b[2] - b[4] < b[12] /*-b[14]*/ + num_vpix) {
- text_open_screen ();
- }
- else if (b[6] - b[14] > -num_vpix) {
- (void) art_swix (Wimp_GetWindowInfo, &r);
- (void) art_swix (Wimp_OpenWindow, &r);
- }
- } /* text_scroll */
- #endif
-